package github.flandre.examplemod.core.registry;

import github.flandre.examplemod.core.init.EnchantmentInit;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.world.Explosion;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.entity.living.LivingDeathEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)//注册到forge中
public class RegisterEnchament {
    @SubscribeEvent
    public static void onLivingDeath(LivingDeathEvent event){
        Entity source =  event.getSource().getImmediateSource(); //获得对他造成伤害的人
        //判断是否为玩家
        //instanceof是Java中的二元运算符，左边是对象，右边是类；当对象是右边类或子类所创建对象时，返回true；否则，返回false。
        if(source instanceof PlayerEntity)
        {
            //获取玩家
            PlayerEntity player = (PlayerEntity) source;
            //获取物品
            ItemStack heldItemMainhand = player.getHeldItemMainhand();
            //ctrl + alt + v 快速补全变量名
            //通过附魔类获取附魔等级
            int Level = EnchantmentHelper.getEnchantmentLevel(EnchantmentInit.EXAMPLE_ENCHANTMENT.get(), heldItemMainhand);
            if(Level > 0)
            {
                ((PlayerEntity) source).addPotionEffect(new EffectInstance(Effects.RESISTANCE,2,3));
                //获取触发的实体
                Entity entity = event.getEntity();
                entity.world.createExplosion(null,
                        entity.getPosX(),entity.getPosY(),entity.getPosZ(),
                        Level,  //爆炸等级
                        Explosion.Mode.NONE //是否破坏方块
                );
            }

        }
    }
}
